(function() {
    var defaults = {
        title: '',
        content: '',
        //btns: [],
        width: 450,
        height: 300,
        drag: true,
        fixed: true,
        modal: true,
        hideFoot: false,
        autoInit: true,
        autoDestory: false
    }, callback = Tgm.callback;
    var Window = function(opts) {
        this.id = Tgm.id();
        $.extend(this, defaults, opts);
        if (this.className) {
            this.className = '-' + this.className;
        } else {
            this.className = '';
        }
        var obj = $.extend({}, this);
        this.get = function(key) {
            return obj[key];
        };
        this.set = function(key, value) {
            return obj[key] = value;
        };
        if (this.autoInit) {
            this.init();
        }
    };
    Window.prototype = (function() {
        var helper = {
            init: {
                action: function() {
                    var t = this;
                    helper.init.html.call(t);
                    helper.init.node.call(t);
                    helper.init.style.call(t);
                    helper.init.event.call(t);
                },
                html: function() {
                    var t = this, html = '';
                    html += '<div class="mmc-window' + t.get('className') + '" id="' + t.get('id') + '">';
                    html += '  <div class="mw-header">';
                    html += '      <div class="mw-title">' + t.get('title') + '</div>';
                    html += '  </div>';
                    html += '  <div class="mw-body">' + t.get('content') + '</div>';
                    html += '  <div class="mw-footer">';
                    if (t.get('buttons')) {
                        $.each(t.get('buttons'), function(i, btn) {
                            html += helper.rule.addBtn.call(t, btn, true);
                        });
                    }
                    html += '  </div>';
                    html += '  <div class="mw-close"><a title="关闭" class="mw-close-btn" href="#"></a></div>';
                    html += '</div>';
                    $(document.body).append(html);
                },
                node: function() {
                    var t = this, winNode;
                    t.set('winNode', $('#' + t.get('id')));
                    winNode = t.get('winNode');
                    t.set('headerNode', winNode.find('.mw-header').eq(0));
                    t.set('titleNode', winNode.find('.mw-title').eq(0));
                    t.set('closeNode', winNode.find('.mw-close').eq(0));
                    t.set('bodyNode', winNode.find('.mw-body').eq(0));
                    t.set('footerNode', winNode.find('.mw-footer').eq(0));
                },
                style: function() {
                    var t = this;
                    t.setModal(t.get('modal'));
                    t.setFixed(t.get('fixed'));
                },
                event: function() {
                    var t = this, closeNode = t.get('closeNode'), buttons = t.get('buttons');
                    closeNode.bind('click', function() {
                        t.unrender();
                        return false;
                    });
                    if (buttons) {
                        $.each(buttons, function(i, btn) {
                            $('#' + btn.id).bind('click', function() {
                                btn.fn && btn.fn.call(t);
                            });
                        });
                    }
                }
            },
            render: {
                action: function() {
                    var t = this, winNode = t.get('winNode'), headerNode = t.get('headerNode'), bodyNode = t.get('bodyNode'), footerNode = t.get('footerNode'), win = $(window), w = t.get('width'), h = t.get('height'), mask = t.get('mask'), fixed = t.get('winFixed');
                    if (t.get('modal')) {
                        mask.render();
                    }
                    winNode.css({
                        'z-index': Tgm.get('zIndex'),
                        display: 'block',
                        top: (win.height() - h) / 3 + win.scrollTop(),
                        left: (win.width() - w) / 2 + win.scrollLeft(),
                        width: w,
                        height: h
                    });
                    if (t.get('hideFoot')) {
                        helper.render.hideFooter.call(t);
                    } else {
                        helper.render.showFooter.call(t);
                    }
                    if (t.get('fixed')) {
                        fixed.render();
                    }
                    if (t.get('drag')) {
                        t.set('winDrag', new Tgm.Drag({
                            node: winNode,
                            handler: headerNode,
                            proxy: false,
                            bdr: function() {
                                if (t.get('fixed')) {
                                    fixed.unrender();
                                }
                            },
                            adrop: function() {
                                if (t.get('fixed')) {
                                    fixed.render();
                                }
                            }
                        }));
                    }
                },
                showFooter: function() {
                    var t = this, headerNode = t.get('headerNode'), bodyNode = t.get('bodyNode'), footerNode = t.get('footerNode'), h = t.get('height');
                    bodyNode.removeClass('mw-body-nofooter');
                    bodyNode.height(h - headerNode.outerHeight() - footerNode.outerHeight());
                    footerNode.show();
                },
                hideFooter: function() {
                    var t = this, headerNode = t.get('headerNode'), bodyNode = t.get('bodyNode'), footerNode = t.get('footerNode'), h = t.get('height');
                    bodyNode.addClass('mw-body-nofooter');
                    bodyNode.height(h - headerNode.outerHeight());
                    footerNode.hide();
                }
            },
            unrender: {
                action: function() {
                    var t = this, winNode = t.get('winNode'), mask = t.get('mask'), bodyNode = t.get('bodyNode'), footerNode = t.get('footerNode');
                    if (t.get('modal')) {
                        mask.unrender();
                    }
                    if (t.get('fixed')) {
                        t.get('winFixed').unrender();
                    }
                    winNode.hide();
                    if (t.get('drag')) {
                        t.get('winDrag').destory();
                    }
                }
            },
            destory: {
                action: function() {
                    var t = this;
                }
            },
            ie6: {},
            rule: {
                addBtn: function(btn, mode) {
                    var t = this, disable = '', style, html, btnId;
                    if (btn.disable) {
                        disable = ' disabled = "true"';
                        style = ' mw-btn-disable';
                    } else {
                        style = ' mw-btn-enable';
                    }
                    btnId = btn.id ? ' id="' + btn.id + '" ' : '';
                    html = '<input type="button" ' + btnId + ' class="mw-btn ' + style + '" value="' + btn.value + '" ' + disable + '>';
                    if (mode) {
                        return html;
                    } else {
                        t.get('footerNode').append(html);
                        if (btn.id) {
                            $('#' + btn.id).bind('click', function() {
                                btn.fn && btn.fn.call(t);
                            });
                        }
                    }
                },
                enableBtn: function(id) {
                    var t = this;
                    $('#' + id).addClass('mw-btn-enable').removeClass('mw-btn-disable').removeAttr('disabled');
                },
                disableBtn: function(id) {
                    var t = this;
                    $('#' + id).addClass('mw-btn-disable').removeClass('mw-btn-enable').attr('disabled', true);
                },
                sysWinComm: function(params) {
                    var t = this, content = '<div class="mw-tip-wrap';
                    if (params.title) {
                        t.setTitle(params.title);
                    }
                    if (params.icon) {
                        content += ' mw-' + params.icon;
                    }
                    content += '">';
                    content += params.content;
                    content += '</div>';
                    this.setContent(content).render().setWH({
                        width: params.width || 375,
                        height: params.height || 175
                    });
                    this.get('footerNode').html('');
					if(params.hideFoot) {
						this.hideFooter();
					}
                }
            },
            event: (function() {
                return {
                    bind: {},
                    unbind: {}
                };
            })()
        };
        return {
            constructor: Window,
            init: function() {
                if (this.get('isInit')) {
                    return this;
                } else {
                    if (callback(this.get('bi'), this.get('bip'), this.get('bis') || this) !== false) {
                        helper.init.action.apply(this);
                        this.set('isInit', true);
                        callback(this.get('ai'), this.get('aip'), this.get('ais') || this);
                        return this;
                    }
                    return false;
                }
            },
            render: function() {
                if (!this.get('isInit')) {
                    this.init();
                }
                if (this.get('isRender')) {
                    return this;
                } else {
                    if (callback(this.get('br'), this.get('brp'), this.get('brs') || this) !== false) {
                        helper.render.action.apply(this);
                        this.set('isRender', true);
                        callback(this.get('ar'), this.get('arp'), this.get('ars') || this);
                        return this;
                    }
                }
            },
            unrender: function() {
                if (this.get('isRender')) {
                    if (callback(this.get('bc'), this.get('bcp'), this.get('bcs') || this) !== false) {
                        helper.unrender.action.apply(this);
                        this.set('isRender', false);
                        callback(this.get('ac'), this.get('acp'), this.get('acs') || this);
                        if (this.get('autoDestory')) {
                            this.destory();
                        }
                        return this;
                    }
                } else {
                    return false;
                }
            },
            destory: function() {
                if (this.get('isInit')) {
                    if (callback(this.get('bd'), this.get('bdp'), this.get('bds') || this) !== false) {
                        helper.destory.action.apply(this);
                        this.set('isInit', false);
                        callback(this.get('ad'), this.get('adp'), this.get('ads') || this);
                        return this;
                    }
                } else {
                    return false;
                }
            },
            setThemes: function(themes) {
                this.set('themes', themes);
                this.unrender();
                if (themes) {
                    this.get('winNode').removeClass().addClass('mmc-window-' + themes);
                } else {
                    this.get('winNode').removeClass().addClass('mmc-window');
                }
                this.render();
            },
            setWH: function(params) {
                if (this.get('isRender')) {
                    params.width && this.set('width', params.width);
                    params.height && this.set('height', params.height);
                    this.unrender().render();
                }
                return this;
            },
            setTitle: function(title) {
                this.set('title', title);
                this.get('titleNode').html(title);
                return this;
            },
            setContent: function(content) {
                this.set('content', content);
                this.get('bodyNode').html(content);
                return this;
            },
            setModal: function(flag) {
                flag = !!flag;
                this.set('modal', flag);
                if (flag) {
                    if (!this.get('mask')) {
                        this.set('mask', new Tgm.Mask({
                            msg: false
                        }));
                    }
                } else {
                    if (this.get('mask')) {
                        this.get('mask').unrender();
                    }
                }
                return this;
            },
            setFixed: function(flag) {
                flag = !!flag;
                this.set('fixed', flag);
                if (flag) {
                    if (!this.get('winFixed')) {
                        this.set('winFixed', new Tgm.Fixed({
                            renderTo: this.get('winNode')
                        }));
                    }
                } else {
                    if (this.get('winFixed')) {
                        this.get('winFixed').unrender();
                    }
                }
                return this;
            },
            addBtn: function(btn) {
                helper.rule.addBtn.call(this, btn);
                return this;
            },
            enableBtn: function(id) {
                helper.rule.enableBtn.call(this, id);
                return this;
            },
            disableBtn: function(id) {
                helper.rule.disableBtn.call(this, id);
                return this;
            },
            alert: function(params) {
                helper.rule.sysWinComm.call(this, params);
                helper.rule.addBtn.call(this, {
                    id: params.fnId || (this.get('id') + 'alertBtn'),
                    value: params.fnText || '确定',
                    fn: function() {
                        this.unrender();
                        params.fn && params.fn.call(this);
                    }
                });
                return this;
            },
            confirm: function(params) {
                helper.rule.sysWinComm.call(this, params);
                helper.rule.addBtn.call(this, {
                    id: params.fnId || (this.get('id') + 'confirmOKBtn'),
                    value: params.fnText || '确定',
                    disable: params.fnDisable || false,
                    fn: function() {
                        params.fn && params.fn.call(this);
                    }
                });
                helper.rule.addBtn.call(this, {
                    id: params.cancelId || (this.get('id') + 'confirmCancelBtn'),
                    value: params.cancelText || '取消',
                    disable: params.cancelDisable || false,
                    fn: function() {
                        params.cancelFn && params.cancelFn.call(this);
                        this.unrender();
                    }
                });
                return this;
            },
            prompt: function() {
            
            },
            hideFooter: function() {
                this.set('hideFoot', true);
                helper.render.hideFooter.call(this);
                return this;
            },
            show: function(params) {
                helper.rule.sysWinComm.call(this, params);
                if (params.btns) {
					var btn;
                    for (var i = 0, len = params.btns.length; i < len; i++) {
						btn = params.btns[i];
                        helper.rule.addBtn.call(this, {
                            id: btn.id,
                            value: btn.text,
                            disable: btn.disable || false,
                            fn: function() {
                                btn.fn && btn.fn.call(this);
                            }
                        });
                    }
                }
                return this;
            }
        };
    })();
    Tgm.reg('Window', Window);
})();
